﻿@model CommentDTO[]
@{
    ViewBag.Title = "List";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<section class="layui-larry-box">
    <div class="larry-personal">
        <div class="layui-tab">
            <blockquote class="layui-elem-quote comments_search">
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-normal commentsAdd_btn">添加评论</a>
                </div>
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-danger batchDel">批量删除</a>
                </div>
                <div class="layui-inline">
                    <div class="layui-form-mid layui-word-aux">本页面刷新后除新添加的文章外所有操作无效，关闭页面所有数据重置</div>
                </div>
                <div class="layui-inline">
                    <span class="layui-form-mid">共有<span style="font-weight: bold"> @Model.Count() </span>条数据</span>
                </div>
            </blockquote>

            <!-- 操作日志 -->
            <div class="layui-form comments_list">
                <input type="hidden" id="Commentcount" name="count" value="@Model.Count()" />
                <table class="layui-table">
                    <colgroup>
                        <col width="10%">
                        <col>
                        <col>
                        <col>
                        <col>
                        <col>
                    </colgroup>
                    <thead>
                        <tr>
                            <th>
                                <input name="" lay-skin="primary" lay-filter="allChoose" id="allChoose" type="checkbox">
                                <div class="layui-unselect layui-form-checkbox" lay-skin="primary">
                                    <i class="layui-icon"></i>
                                </div>
                            </th>
                            <th>评论内容</th>
                            <th>评论星级</th>
                            <th>商品名称</th>
                            <th>用户名称</th>
                            <th>订单号</th>
                            <th>评论图片</th>
                            <th>操作</th>
                        </tr>
                    </thead>

                    <tbody class="comments_content" id="com">

                        @foreach (var comment in Model)
                        {
                            <tr>
                                <td>
                                    <input name="checked" lay-skin="primary" lay-filter="choose" type="checkbox">
                                    <div class="layui-unselect layui-form-checkbox" lay-skin="primary">
                                        <i class="layui-icon"></i>
                                    </div>
                                </td>
                                <td>@comment.Content</td>
                                <td>@comment.Level</td>
                                <td>@comment.GoodName</td>
                                <td>@comment.UserName</td>
                                <td>@comment.OrderId</td>
                                <td><a href="@comment.CommentImgSrc">查看</a></td>
                                <td>
                                    <a class="layui-btn layui-btn-mini comments_edit"><i class="iconfont icon-edit" data-id="@comment.Id"></i> 编辑</a>
                                    <a class="layui-btn layui-btn-danger layui-btn-mini comments_del" data-id="@comment.Id">
                                        <i class="layui-icon"></i> 删除
                                    </a>
                                </td>
                            </tr>
                        }

                    </tbody>
                </table>
                <div class="larry-table-page clearfix">
                    <div id="page" class="page"></div>
                </div>
            </div>

        </div>
    </div>
</section>

@section FooterScript{
    <script id="comment_template" type="text/html">
        {{each comments as comment}}
        <tr>
            <td>
                <input name="checked" lay-skin="primary" lay-filter="choose" type="checkbox" class="cm">
                <div class="layui-unselect layui-form-checkbox" lay-skin="primary"><i class="layui-icon"></i></div>
            </td>

            <td>{{comment.content}}</td>
            <td>{{comment.level}}</td>
            <td>{{comment.goodName}}</td>
            <td>{{comment.userName}}</td>
            <td>{{comment.orderId}}</td>
            <td><a href="#">查看</a></td>
            <td>
                <a class="layui-btn layui-btn-mini comments_edit" data-id="{{comment.Id}}"><i class="iconfont icon-edit"></i> 编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-mini comments_del" data-id="{{comment.id}}">
                    <i class="layui-icon"></i> 删除
                </a>
            </td>

        </tr>
        {{/each}}
    </script>
    <script src="https://cdn.bootcss.com/vue/2.3.3/vue.min.js"></script>

    <script type="text/javascript">
    // 定义一个变量用来存储分页数
        var count = 0;
        var com = document.getElementById("#com");
    $(function () {
        // 获取总数据条数，然后除10，得到的就是页数
        count = parseInt(Math.ceil( @ViewBag.totalCount/ 10));

        // 添加评论
        
    })


    var appConfig = {
        name: "comments",    //需要和页面名称对应
        title: "评论",    //显示的名称
        addUrl: "/Comment/AddNew",   //添加弹框的路径
        editUrl: "/Comment/Edit?id=",      //修改弹框的路径
        $: "",
        layer: "",


        // 批量删除Ajax
        batchDel: function (data) {     //Array data 得到的是 需要删除的id
            $ = appConfig.$
            $.ajax({
                url: "/Comment/BatchDel",
                method: "post",
                dataType: "json",
                data: { "commentIds": data },    //$(data)要删除的数组
                traditional: true,//这里设置为true
                success: function (res) {
                    if (res.status == "ok") {
                        appConfig.layer.msg("删除成功", {
                            icon: 1, time: 1000, end: function () {
                                location.reload();
                            }
                        });



                    } else {
                        appConfig.layer.msg("删除失败", {
                            icon: 5, time: 1000, end: function () {
                                location.reload();
                            }
                        });
                    }
                },
                error: function () {
                    appConfig.layer.msg("删除失败", {
                        icon: 5, time: 2000, end: function () {
                            location.reload();
                        }
                    });

                    location.reload();
                }
            });

        },
        // 软删除
        del: function (data) {

            console.log(data);
            $ = appConfig.$
            //todo:在此处添加删除记录的ajax
            $.ajax({
                url: "/Comment/Delete/",
                method: "post",
                dataType: "json",
                data: { "commentId": data },
                success: function (res) {
                    if (res.status == "ok") {
                        appConfig.layer.msg("删除成功", { icon: 1 });

                    } else {
                        appConfig.layer.msg("删除失败", { icon: 5 });
                    }
                },
                error: function () {
                    appConfig.layer.msg("删除失败", { icon: 5 });
                }
            });
        },

        
        
    };
    layui.use(['jquery', 'layer', 'element', 'laypage','form'], function () {
        window.jQuery = window.$ = layui.jquery;
        window.layer = layui.layer;
        var element = layui.element(),
            laypage = layui.laypage;
        var form = layui.form();

        laypage({
            cont: 'page',
            pages: count //总页数
            ,
            groups: 5 //连续显示分页数
            ,
            jump: function (obj, first) {
                //得到了当前页，用于向服务端请求对应数据
                var curr = obj.curr; // 获取当前页数下标

                if (!first) {
                    $.ajax({
                        type: "post",
                        url: "/Comment/List?index=" + curr,
                        success: function (data) {
                  
                            //var html = template.render("comment_template",data )
                            //var html = template("comment_template", { comments: data });
                            //document.getElementById('com').innerHTML = html;
                            
                            var html = template("comment_template", { comments: data });
                            $("#com").html(html);
                           form.render('checkbox');
                        },
                        error: function () {
                            alert("error")
                        }
                    })
                    //layer.msg('第 '+ obj.curr +' 页');
                }
            }
        });

    });
    </script>
}

